package com.zabra.zl.ui.allocation_plan;

import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.orhanobut.logger.Logger;
import com.xujiaji.happybubble.BubbleDialog;
import com.zabra.zl.Config;
import com.zabra.zl.R;
import com.zabra.zl.base.BaseMvpActivity;
import com.zabra.zl.data.EventMessage;
import com.zabra.zl.data.MoneyRuleBean;
import com.zabra.zl.data.MultipleRuleBean;
import com.zabra.zl.net.APIService;
import com.zabra.zl.ui.allocation_plan.contract.AllocationPlanContract;
import com.zabra.zl.ui.allocation_plan.presenter.AllocationPlanPresenter;
import com.zabra.zl.ui.free_interset.FreeintersetActivity;
import com.zabra.zl.ui.nullpage.NullPersonActivity;
import com.zabra.zl.utils.MaptoJsonUtils;
import com.zabra.zl.utils.ShowDialogUtils;
import com.zabra.zl.utils.ToastUtil;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnCheckedChanged;
import butterknife.OnClick;
import butterknife.OnTextChanged;

/**
 * 配置方案
 */
public class AllocationPlanActivity extends BaseMvpActivity<AllocationPlanPresenter> implements AllocationPlanContract.View {


    @BindView(R.id.back)
    LinearLayout back;
    @BindView(R.id.et_money)
    EditText etMoney;
    @BindView(R.id.recycler)
    RecyclerView recycler;
    @BindView(R.id.bt_next)
    TextView btNext;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.rb_immediately)
    RadioButton rbImmediately;
    @BindView(R.id.rb_nextmon)
    RadioButton rbNextmon;
    private List<MultipleRuleBean.RespDataBean> list;
    private Adp adp;
    private Bundle bundle;
    private int strIndex, bei, bpid, tiemType = 1;
    private int pos = -1;
    private String strMoney = "0", strlilv;

    @Override
    public int getLayoutId() {
        return R.layout.activity_allocation_plan;
    }

    @Override
    public void initView() {
        bundle = getIntent().getExtras();
        strIndex = bundle.getInt("mIndex");
        setTitle();
        mPresenter = new AllocationPlanPresenter();
        mPresenter.attachView(this);
        //获取金额规则
        mPresenter.getMoneyRuleData(strIndex);
        //获取倍数规则
        mPresenter.getMultipleRuleData(strIndex);


    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void onError(Throwable throwable) {

    }

    @Override
    public void onSuccess(MoneyRuleBean bean) {
        if (bean.getRespCode().equals(Config.SUCCESS)) {
//            备注：最少10 元，最多 2000元，且为 100 的倍数。
            double minAmt = bean.getRespData().getMinAmt();
            double maxAmt = bean.getRespData().getMaxAmt();
            etMoney.setHint(minAmt + "元~" + maxAmt + "元(且为 100 的倍)。");
        } else if (bean.getRespCode().equals(Config.TOKENOUT)) {
            ShowDialogUtils.restLoginDialog(this);
        } else if (bean.getRespCode().equals(Config.NONAME)) {
            EventMessage message = new EventMessage(1, "Main");
            EventBus.getDefault().postSticky(message);
            toClass1(this, NullPersonActivity.class);
        } else {
            ToastUtil.showToast(this, bean.getRespDesc());
        }
    }

    @OnTextChanged(R.id.et_money)
    public void onTextChanged(CharSequence text) {
        //保证金
        strMoney = etMoney.getText().toString().trim();
        if (TextUtils.isEmpty(strMoney)) {
            strMoney = "0";
        }
        adp.setMoney(strMoney);
    }


    @Override
    public void onSuccess(MultipleRuleBean bean) {
        if (bean.getRespCode().equals(Config.SUCCESS)) {
            list = bean.getRespData();
            recycler.setLayoutManager(new GridLayoutManager(this, 4, LinearLayoutManager.VERTICAL, false));
            recycler.setNestedScrollingEnabled(false);
            adp = new Adp(R.layout.item_allocation, list, strMoney);
            recycler.setAdapter(adp);
            adp.openLoadAnimation();
            adp.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                    pos = position;
                    adp.setSelectItem(position);
                    adp.notifyDataSetChanged();
                    //利率
                    strlilv = list.get(position).getSingleRate() + "";
                    //倍数
                    bei = list.get(position).getTimes();
                    bpid = list.get(position).getBpId();

                    if (pos == -1) {
                        //没有选中
                        btNext.setAlpha(0.5f);
                        btNext.setEnabled(false);
                    } else {
                        //选中
                        btNext.setAlpha(1);
                        btNext.setEnabled(true);
                    }
                }
            });
        } else {
            ToastUtil.showToast(this, bean.getRespDesc());
        }
    }

    @OnCheckedChanged({R.id.rb_immediately, R.id.rb_nextmon})
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        switch (buttonView.getId()) {
            case R.id.rb_immediately:
                if (isChecked) {
                    //立即生效
                    tiemType = 1;
                }
                break;
            case R.id.rb_nextmon:
                if (isChecked) {
                    //下个交易日
                    tiemType = 2;
                }
                break;
        }


    }

    @OnClick({R.id.back, R.id.bt_next})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.bt_next:
                //配资基金
                if (strMoney == "0") {
                    ToastUtil.showToast(this, "请输入保证金");
                } else {
                    double peizi = Double.parseDouble(strMoney) * bei;
                    bundle = new Bundle();
                    bundle.putInt("mIndex", strIndex);
                    bundle.putString("lilv", strlilv);
                    bundle.putString("peizi", peizi + "");
                    bundle.putString("baozheng", strMoney);
                    bundle.putInt("timetype", tiemType);
                    bundle.putInt("bpid", bpid);
                    bundle.putInt("bei", bei);
                    toClass(this, FreeintersetActivity.class, bundle);
                }

                break;
        }
    }

    private void setTitle() {
        switch (strIndex) {
            case Config.FREE:
                tvTitle.setText("免息");
                break;
            case Config.DAY:
                tvTitle.setText("按日");
                break;
            case Config.MON:
                tvTitle.setText("按月");
                break;
        }
    }

    private void text(){
    }

}
